package cn.com.bluemoon.daps.model.core.file;

import cn.com.bluemoon.daps.common.domain.BaseModel;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.google.common.collect.Lists;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * @author Jarod.Kong
 * @date 2021/3/8 16:50
 */
@ToString
@ExcelIgnoreUnannotated
public abstract class IMetaData<T extends BaseModel<T>> implements ICopyDbModel<T> {

    // 0开始，非excel的行，需要考虑头行数
    @Setter
    @Getter
    private Integer rowIndex;
    @Setter
    @Getter
    private Integer headRowNumber;
    @Setter
    @Getter
    @ExcelIgnore
    private List<String> errorMsgs = Lists.newArrayList();

    private boolean fail;
    @Setter
    @Getter
    private String tableName;
    @Setter
    @Getter
    private String key;
    // 输出输入排序
    @Setter
    @Getter
    private Integer order;

    public IMetaData() {

    }

    /**
     * 去掉子类的属性数据（简化返回json结构）
     *
     * @param m   子类
     * @param <T> x
     * @return 基类
     */
    public static <T extends IMetaData> IMetaData simpleMetadata(T m) {
        IMetaData iMetaData = new BaseMetaData();
        BeanUtils.copyProperties(m, iMetaData);
        return iMetaData;
    }

    /**
     * 描述Excel sheet名称
     *
     * @return
     */
    public abstract String sheetName();

    /**
     * @return sheetNo
     */
    public abstract Integer sheetNo();

    public boolean isFail() {
        return fail && !getErrorMsgs().isEmpty();
    }

    public void setFail(boolean fail) {
        this.fail = fail;
    }

    private static class BaseMetaData extends IMetaData {

        @Override
        public String sheetName() {
            return null;
        }

        @Override
        public Integer sheetNo() {
            return null;
        }

        @Override
        public IMetaData copyDbProperties0(BaseModel model) {
            return null;
        }
    }
}
